/*==================================================================================================
  wbRainbow (c) 2009 by Wim Bekker. MIT style license.
  
  20090102      Wim     Created
==================================================================================================*/
/*global window document Color $ $$ MooRainbow Fx TWbColors console alert
  borderBackgroundColor
  wrapBackgroundColor
  wrapTextColor 
*/


window.addEvent('domready', function ()
{
  try
  {
    function changeStyleSheet(styleSheetName, selector, name, value)
    {
      var styleSheets = document.styleSheets;
      for (var k = 0; k <  styleSheets.length; k += 1)
      {
        var styleSheet = document.styleSheets[k];
        if (styleSheet.href === styleSheetName)
        {
          var rules = styleSheet.cssRules;
          for (var i = 0; i < rules.length; i += 1)
          {
            var rule = rules[i];
            if (rule.selectorText === selector)
            {
              var style = rule.style;
//              console.log(style[name]);
              if ($defined(style[name]))
              {
                style[name] = value;
//                console.log('set:' + style[name]);
              }
            }
          }
        }
      }
    }
    
    function displayStyleSheet(styleSheetName, selector, name)
    {
      var styleSheets = document.styleSheets;
      for (var k = 0; k <  styleSheets.length; k += 1)
      {
        var styleSheet = document.styleSheets[k];
        if (styleSheet.href === styleSheetName)
        {
          var rules = styleSheet.cssRules;
          for (var i = 0; i < rules.length; i += 1)
          {
            var rule = rules[i];
            if (rule.selectorText === selector)
            {
              var style = rule.style;
              console.log(style[name]);
            }
          }
        }
      }
    }
    
    $('controlhandle').addEvent('click', function ()
    {
      var styles = $('controlpanel').getStyles('top');
      var begin = -51;
      var end = 10;
      if (styles.top === '10px')
      {
        end = begin;
        begin = 10;
      }
      var x = new Fx.Style('controlpanel', 'top', {duration: 400}).start(begin, end);
      x = null;
    });
    
    var rbForeGround = null;
    var rbBackGround = null;
    var rbText = null;
    var ipBackground = null;
    var ipForeground = null;
    var ipText = null;
    var outer = null;
    var wrap = null;
    var logoimg = null;
    var seColorSelector = null;
    var ipColorValue = null;
    var rbColor = null;
    var wbColors = new TWbColors();
    
    function setCookie(sName, sValue, nDays) 
    {
      var expires = "";
      
      if (nDays) 
      {
        var d = new Date();
        d.setTime(d.getTime() + nDays * 24 * 60 * 60 * 1000);
        expires = "; expires=" + d.toGMTString();
      }
    	document.cookie = sName + "=" + sValue + expires + "; path=/";
    }
    
  
    function invert(hex)
    {
      var col = hex.hexToRgb(true);
      var color = new Color(col, 'rgb');
      return color.invert().hex;
    }
    
    function colorFromDesc(source)
    {
      //wbDescxxx -> wbColorxxx
      var result = source.replace(/Desc/, "Color");
      return result;    
    }  
    function descFromColor(source)
    {
      //wbDescxxx -> wbColorxxx
      var result = source.replace(/Color/, "Desc");
      return result;    
    }  
    
    
    function selectedColorChange(setManual)
    {
      var value = wbColors.getValue();
      ipColorValue.value = value;
      ipColorValue.setStyle('backgroundColor' , value);
      ipColorValue.setStyle('color' , invert(value));
      seColorSelector.value = descFromColor(wbColors.selectedColor);
      setCookie(wbColors.selectedColor, value, 3650);
    
      if (setManual)
      {
        rbColor.manualSet(value.hexToRgb(true));
      }

      switch (wbColors.selectedColor)
      {
      case wbColors.constants.wbColorPageBackground             : 
        //  outer.setStyle('backgroundColor' , value);
        changeStyleSheet(null, 'body', 'backgroundColor', value);
        break;
      case wbColors.constants.wbColorContentBackground          :     
        //wrap.setStyle('backgroundColor' , value);
        changeStyleSheet(null, '#wrap', 'backgroundColor', value);
        changeStyleSheet(null, 'div.controlpanel', 'backgroundColor', value);
        changeStyleSheet(null, '#controlhandle', 'backgroundColor', value);
        break;
      case wbColors.constants.wbColorContentText                :
        //wrap.setStyle('color' , value);
        changeStyleSheet(null, '#wrap', 'color', value);
        changeStyleSheet(null, 'div.controlpanel', 'color', value);
           
        break;
      case wbColors.constants.wbColorDefaultLink                :
        changeStyleSheet(null, 'a', 'color', value);
//        $$('a').each(function (element)
//        {
//          if (element.getProperty('class') === '')
//          {
//            element.setStyle('color', value);
//          } 
//        });           
        break;
      case wbColors.constants.wbColorModuleHeaderBackground     :
        changeStyleSheet(null, '.moduletable h3', 'backgroundColor', value); 
        break;
      case wbColors.constants.wbColorMenuHeaderBackground       :  
        changeStyleSheet(null, '.moduletable_menu h3', 'backgroundColor', value); 
        break;
      case wbColors.constants.wbColorTextHeaderBackground       : 
        changeStyleSheet(null, '.moduletable_text h3', 'backgroundColor', value); 
        break;
      case wbColors.constants.wbColorMenuBackground:
        changeStyleSheet(null, '.moduletable_wbMenu, .moduletable_wbMenu a', 'backgroundColor', value);
        break;
      }
    } 
  
    seColorSelector = $('colorselector');
    ipColorValue = $('colorvalue');
    outer = $('outer');
    wrap = $('wrap');
    logoimg = $('logoimg');
    
    
    rbColor = new MooRainbow('rbColor', 
    {
      wheel: true,
      id: 'theRbColor',
      startColor: wbColors.getValue().hexToRgb(true),
      imgPath : '/templates/wb_first/images/moor/',
      align: 'right',
      onChange: function (color) 
      {
        wbColors.setValue(color.hex);
        selectedColorChange(false);
      }
    });
    rbColor.layout.makeDraggable(); 
  
    selectedColorChange(true);
    
    ipColorValue.addEvent('keyup', function ()
    {
      wbColors.setValue(ipColorValue.value);
      selectedColorChange(true);
            
    }.bind(this));
    
    
    $('colorselector').addEvent('change', function (event)
    {
      var name = colorFromDesc(event.target.value);
      wbColors.selectedColor = name;
      selectedColorChange(true);
    });
  }
  catch (e)
  {
    alert('something went terrible wrong: ' + e);
  }
});